Attribute VB_Name = "MColWidth"
Public g_lngDefaultHandler As Long ' Original handler of the grid events
Private m_bLMousePressed As Boolean 'true if the left button is pressed
Private m_bLMouseClicked As Boolean 'true just after the click (i.e. just after the left button is released)

'API declarations ================================================== ==========
' Function to retrieve the address of the current Message-Handling routine
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
' Function to define the address of the Message-Handling routine
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
' Function to execute a function residing at a specific memory address
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

'Windows messages constants
Private Const WM_LBUTTONUP = &H202
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_ERASEBKGND = &H14
'this is our event handler
Public Function GridMessage(ByVal hwnd As Long, _
                            ByVal Msg As Long, _
                            ByVal wp As Long, _
                            ByVal lp As Long) As Long
    If m_bLMousePressed And Msg = WM_LBUTTONUP Then
        'button have been just released
        m_bLMousePressed = False
        m_bLMouseClicked = True
    End If
    
    If Not (m_bLMousePressed) And Msg = WM_LBUTTONDOWN Then
        'button have been just pressed
        m_bLMousePressed = True
        m_bLMouseClicked = False
    End If
    
    If m_bLMouseClicked And (Msg = WM_ERASEBKGND) Then
        'Only when resize happens this event may occur after releasing the button !
        'When user is making a simple click on grid,
        'the WM_ERASEBKGND event occurs before WM_LBUTTONUP,
        'and therefore will not be handled there
        
        Debug.Print "Grid message: ", "Resized !" 'TO DO: Replace this futile code
        'with something usefull
        
        m_bLMouseClicked = False
    
    End If
    
    'call the default message handler
    GridMessage = CallWindowProc(g_lngDefaultHandler, hwnd, Msg, wp, lp)
End Function
